package com.atcc.test.controller;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atcc.test.common.PatternValidater;
import com.atcc.test.common.ResultUtil;
import com.atcc.test.common.TokenHelper;
import com.atcc.test.domain.CommonResult;
import com.atcc.test.domain.TokenModel;
import com.atcc.test.domain.UserInfo;
import com.atcc.test.service.UserInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(tags= {"用户登录接口"})
@RequestMapping("/UserLogin")
public class UserLoginController {
	
	private Logger logger = LoggerFactory.getLogger(UserLoginController.class);
			
     
	@Autowired
	UserInfoService userInfoService;
	
	@ApiOperation(value="获取token信息",notes="获取token信息")
	@GetMapping("/Login")
	public CommonResult<Object> getTokenByPhoneNumber(
			@NotEmpty(message="密码不能为空")
			@RequestParam("password") String password,
			@NotEmpty(message="手机号不能为空")
			@Pattern(regexp=PatternValidater.sjh,message="手机号格式不正确")
			@RequestParam("phoneNumber") String phoneNumber,
			@RequestParam("name") String name,
			@RequestParam(value="isphone",defaultValue="false") boolean isPhone
			) {
		  UserInfo userInfo = userInfoService.getUserInfoByPhoneNumber(phoneNumber);
		  if(userInfo == null) {
			  return ResultUtil.returnError();
		  }else {
			  if(password.equals(userInfo.getPassword())) {
				  logger.info("即将开始获取token");
				  TokenModel tokenModel = TokenHelper.getInstance().create(userInfo, isPhone);
				  System.out.println(tokenModel);
				  return ResultUtil.returnSuccess(tokenModel);
			  }
			  else {
				  return  ResultUtil.returnError(); 
			  } 
		  }
		
		
	}
}
